package com.ksd.common.excel;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;

public class MusicWordTool {
    public static String serverReadFile(String filePath){
        StringBuffer stringBuffer= new StringBuffer();
        try {
            // 对文件名进行编码
            String encodedUrl = encodeUrl(filePath);

            // 创建 URL 对象
            URL url = new URL(encodedUrl);
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            connection.setRequestMethod("GET");
            BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
            String line;
            while ((line = reader.readLine()) != null) {
                stringBuffer.append(line);
            }
            reader.close();
            connection.disconnect();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return stringBuffer.toString();
    }
    private static String encodeUrl(String originalUrl) throws IOException {
        String[] parts = originalUrl.split("/");
        StringBuilder encodedUrl = new StringBuilder();
        for (int i = 0; i < parts.length; i++) {
            if (i > 0) {
                encodedUrl.append("/");
            }
            if (i == parts.length - 1) {
                // 对文件名进行编码
                encodedUrl.append(URLEncoder.encode(parts[i], "UTF-8"));
            } else {
                encodedUrl.append(parts[i]);
            }
        }
        return encodedUrl.toString().replace("+", "%20");
    }
}
